package org.jtzc.springaimcpclient;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpSchema;

import java.util.Map;

/**
 * @author wu chuang
 * @description
 */
public class DemoStdio {
    public static void main(String[] args) {
        // 客户端代码示例
        var stdioParams = ServerParameters.builder("java")
                // 设置必要的系统属性
                .args("-Dspring.ai.mcp.server.stdio=true",
                        "-Dspring.main.web-application-type=none",
                        "-Dlogging.pattern.console=",
                        "-jar",
                        "/Users/wufagang/tj/spring-ai-mcp/spring-ai-mcp-server-stdio/target/spring-ai-mcp-server-stdio-0.0.1-SNAPSHOT.jar")
                .build();

        // 创建基于stdio的传输层
        var transport = new StdioClientTransport(stdioParams);
        // 构建同步MCP客户端
        var client = McpClient.sync(transport).build();

        // 初始化客户端连接
        client.initialize();

        // 调用天气预报工具
        McpSchema.CallToolResult weatherResult = client.callTool(
                new McpSchema.CallToolRequest("getWeatherForecastByLocation",
                        Map.of("latitude", "39.9042", "longitude", "116.4074"))
        );

        // 打印结果
        System.out.println(weatherResult.content());
    }
}
